<?php

namespace Common\Model;

use Think\Model\RelationModel;

class VUserTaskModel extends RelationModel
{
    protected $_link = array(
        'user_task_annex' => array(
            'mapping_type' => self::HAS_MANY,
            'class_name' => 'UserTaskAnnex',
            'foreign_key' => 'user_task_id',
        ),
    );

    public function get($id)
    {
        $result = $this->where('id="%d"', $id)->find();
        return $result;
    }

    public function read($id)
    {
        $data['status'] = 1;
        $result = $this->where('id=%d', $id)->save($data);
        if ($result) {
            return true;
        }
        return false;
    }

    public function get_list($user_id, $search_key, $status, $task_type, $task_id, $rank, $attribute, $page_no = 1, $page_size = 0)
    {
        $condition = $this->assembleCondition($user_id, $search_key, $status, $task_type, $task_id, $rank, $attribute);
        $count = 0;

        if ($page_size > 0) {
            $list = $this->relation(true)->where($condition)->order('amount_score desc')->limit(($page_no - 1) * $page_size, $page_size)->select();
            $count = $this->where($condition)->count();
        } else {
            $list = $this->relation(true)->where($condition)->order('amount_score desc')->select();
        }
        return [$list, $count];
    }

    private function assembleCondition($user_id, $search_key, $status, $task_type, $task_id, $rank, $attribute)
    {

        $condition = array();
        if (!empty($user_id)) {
            $condition['user_id'] = array('eq', $user_id);
        }
        if (!empty($search_key)) {
            $condition['account'] = array('like', '%' . $search_key . '%');
        }
        if (is_numeric($task_type)) {
            $condition['task_type'] = array('eq', $task_type);
        }
        if (!empty($task_id)) {
            $condition['task_id'] = array('eq', $task_id);
        }
        if ($status != "" && $status != null) {
            if (is_array($status)) {
                $condition['status'] = array('in', $status);
            } else {
                $condition['status'] = array('eq', $status);
            }
        } else {
            $condition['status'] = array('egt', 0);
        }
        if ($rank != "" && $rank != null) {
            $condition['rank'] = array('eq', $rank);
        }

        if ($attribute != "" && $attribute != null) {
            $condition['attribute'] = array('eq', $attribute);
        }
        $condition['bstatus'] = array('egt', 0);
        return $condition;
    }

    public function statistics($year, $task_type, $user_id = null)
    {
        $condition = array();
        if (!empty($user_id)) {
            $condition['user_id'] = array('eq', $user_id);
        }
        if (!empty($task_type)) {
            $condition['task_type'] = array('eq', $task_type);
        }

        $list = $this->where($condition)->field("user_id,sum(amount_score) as amount_score")->group("user_id")->order('user_id asc')->select();
        return $list;
    }
}